<?php
session_start();
$_SESSION['debug']=false;
if($_SESSION['debug']){
	$_SESSION['auth'] = true;	//DEBUG
	$_SESSION['rule'] = "PROVIDER";//DEBUG
}
if ($_SESSION['auth'] != true || ($_SESSION['rule'] != "PROVIDER" && $_SESSION['rule'] != "PUBLISHER")) {
	die('Not log in');
}
include("includes/adminController.php");

if(empty($_GET['id'])){
echo "error: channel id not found";
exit;
}
$channels = searchChannel($_GET['id'],null,null,null,null,null,null,null,null,null,null,null,null);
$channel = $channels[0];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="itelement.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="script/jquery.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready (
	function () {
		$('#addItemBefore').click(addItemBefore);
		$('#addItem').click(addItem);
	}
);
function addItemBefore(){
	var err = itemReq();
	if(err != ''){
		$('#status').show('fast');
		$('#status').html(err);
		return;
	}
	$('#status').show(200);
	$('#status').text('Waiting server');
	var serialize = $('form').serialize();//$('form').serializeArray( );
	serialize = serialize.replace(/description/, "nothing");
	serialize += "&description="+escape(tinyMCE.getContent());
	var desc = escape(tinyMCE.getContent());
	$.post("provider_operation.php?action=add_item_before_attach",serialize,
	  function(data){
		window.location.href = 'provider_edit_item2.php?id='+data;
	  });

}
function addItem(){
	var err = itemReq();
	if(err != ''){
		$('#status').text(err);
		return;
	}
	$('#status').show(200);
	$('#status').text('Waiting server');
	var serialize = $('form').serialize();//$('form').serializeArray( );
	serialize = serialize.replace(/description/, "nothing");
	serialize += "&description="+escape(tinyMCE.getContent());
	var desc = escape(tinyMCE.getContent());
	$.post("provider_operation.php?action=add_item",serialize, 
	  function(data){
		window.location.href = 'provider_edit_item2.php?id='+data;
	  });

}
function itemReq(){
	var br = '<br />';
	var err = '';
	if($('#title').val()==''){
		err += 'o  Title field requires'+br;
	}
	if(err != '')err = err.substring(0,err.length-br.length);
	return err;
}

</script>
<!-- TinyMCE -->
<script language="javascript" type="text/javascript" src="script/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		plugins : "table,advimage,advlink,contextmenu,paste,fullscreen",
		theme_advanced_buttons1_add : "fontselect",
		theme_advanced_buttons2_add : "separator,forecolor,backcolor",
		theme_advanced_buttons2_add_before: "pastetext,pasteword,separator",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons3_add : "advhr,separator,fullscreen",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		content_css : "example_word.css",
		external_link_list_url : "example_link_list.js",
		external_image_list_url : "example_image_list.js",
		media_external_list_url : "example_media_list.js",
		file_browser_callback : "fileBrowserCallBack",
		paste_use_dialog : false,
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		theme_advanced_link_targets : "_something=My somthing;_something2=My somthing2;_something3=My somthing3;",
		paste_auto_cleanup_on_paste : true,
		paste_convert_headers_to_strong : false,
		paste_strip_class_attributes : "all",
		paste_remove_spans : false,
		paste_remove_styles : false		
	});

	function fileBrowserCallBack(field_name, url, type, win) {
		// This is where you insert your custom filebrowser logic
		alert("Filebrowser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);

		// Insert new URL, this would normaly be done in a popup
		win.document.forms[0].elements[field_name].value = "someurl.htm";
	}
</script>
<!-- /TinyMCE -->
</head>

<body>
<form>
	<div class="viewer-head">Add item to '<?php echo $channel['title']; ?>' </div>
	<div class="input">
		<div class="title">Title *</div>
		<div><input style="font-size:large;width:100%" id="title" name="title" type="text"></div>
	</div>
	<!--<div class="input">
			GUID
			  <div>
			  <input style="width:100%" name="guid" type="text">
			</div>
		  </div>-->
	<div class="input">
		<div class="title">Attachment</div>
		<div  id="attachments">
			<div id="attachment">
			<a href="#" id="addItemBefore" style="font-size:12px;">Click to attach file.</a>
			</div>
		</div>
	</div>
	<div class="input">
		<div class="title">Content</div>
		<div>        
		<textarea id="elm1" id="description" rows="1" cols="80" style="width:50%">
		</textarea></div>
	</div>
	<input type="hidden" name="channelid" value="<?php echo $_GET['id']; ?>" />
	<div id="status"></div>
	<input type="button" name="save" value="Post" id="addItem"/>
</form>
</body>
</html>
